package historymanager.servlets;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

import historymanager.tools.Configuration;
import historymanager.tools.CsvUpdate;
/**
 * 
 * Changed by Thomas Aulich 29.03.2015:<br/>
 * - Marked with TA01: Connection handling ... </br>
 * 
 * @author Marcel Bardehle
 *
 */
public class ProgramInfo {

	private static ProgramInfo instance;
	private Date myBirthDate;
	static Logger log = Logger.getLogger(CsvUpdate.class);
	public Connection con;
	public Statement stmt1;
	ResultSet rs1;
	int measurepointCount;
	long timeLastRequest;
	String databaseStatus;
	String dateLastRequest;
	long requestInterval;
	private double databasefreeSpace;
	private double databaseDataSpace;
	private double databaseIndexSpace;
	private double databaseCompleteSpace;
	private double databaseMinFreeSpace;
	Configuration myConfiguration;
	
	
	private ProgramInfo() {
		myBirthDate = new Date();
	}

	public static ProgramInfo getInstance() {
		if (instance == null) {
			// Singleton with double-checked locking
			synchronized (ProgramInfo.class) {
				if (instance == null) {
					instance = new ProgramInfo();
				}
			}
		}
		return instance;
	}

	public Date getBirthDate() {
		return myBirthDate;
	}

	public int getmeasurepointCount() {
		return measurepointCount;
	}

	public long gettimeLastRequest() {
		return timeLastRequest;
	}

	public String getdatabaseStatus() {
		return databaseStatus;
	}

	public void setmeasurepointCount(int measurePoint) {
		measurepointCount = measurePoint;
	}

	public void settimeLastRequest(long lastRequest) {
		timeLastRequest = lastRequest;
	}

	public void setdatabaseStatus(String Status) {
		databaseStatus = Status;
	}

	public void setDateLastRequest(String dateRequest) {
		dateLastRequest = dateRequest;
	}

	public String getDateLastRequest() {
		return dateLastRequest;
	}

	public void setRequestInterval(long interval) {
		requestInterval = interval / 1000 / 60;// Umrechung in Minuten
	}

	public long getRequestInterval() {
		return requestInterval;
	}

	public void setDatabasefreeSpace(double freespace) {
		databasefreeSpace = freespace;
	}

	public double getDatabasefreeSpace() {
		return databasefreeSpace;
	}

	public void setDatabaseDataSpace(double dataSpace) {
		databaseDataSpace = dataSpace;
	}

	public double getDatabaseDataSpace() {
		return databaseDataSpace;
	}

	public void setDatabaseIndexSpace(double indexSpace) {
		databaseIndexSpace = indexSpace;
	}

	public double getDatabaseIndexSpace() {
		return databaseIndexSpace;
	}

	public void setDatabaseCompleteSpace(double completeSpace) {
		databaseCompleteSpace = completeSpace;
	}

	public double getDatabaseCompleteSpace() {
		return databaseCompleteSpace;
	}

	public void setDatabaseMinFreeSpace(double MinFreeSpace) {
		databaseMinFreeSpace = MinFreeSpace;
	}

	public double getDatabaseMinFreeSpace() {
		return databaseMinFreeSpace;
	}

	public void setDatabaseSpace() {
		String insertSQL;
		String database = "historymanager";
		myConfiguration = Configuration.getInstance();
		Context envContext;
		insertSQL = "SELECT table_schema AS 'Database',Round( SUM( data_length ) / 1024 / 1024, 3 ) AS 'Data',Round( SUM( index_length ) / 1024 / 1024, 3 ) AS 'Index',Round( Sum( data_length + index_length ) / 1024 / 1024, 3 ) AS 'Complete',Round( Sum( data_free ) / 1024 / 1024, 3 ) AS 'FreeSpace'FROM information_schema.tables GROUP BY table_schema ;";
		try {
			envContext = new InitialContext();
			DataSource ds = (DataSource) envContext.lookup(myConfiguration
					.getDataSourceContext());
			con = ds.getConnection(); // Datenbank-Verbindung erstellen
			stmt1 = con.createStatement();
			rs1 = stmt1.executeQuery(insertSQL);
			while (rs1.next()) { // Datenbank-Speicher abrufen und in
									// ProgramInfo schreiben.
				String databaseName = rs1.getString("Database");
				if (databaseName.equals(database)) {
					setDatabaseDataSpace(Double.valueOf(rs1.getString("Data")));
					setDatabaseIndexSpace(Double
							.valueOf(rs1.getString("Index")));
					setDatabaseCompleteSpace(Double.valueOf(rs1
							.getString("Complete")));
					setDatabasefreeSpace(Double.valueOf(rs1
							.getString("FreeSpace")));
					break;
				}
			}
			// TA01 start -->
			rs1.close();
			stmt1.close();
			con.close();
			log.debug("Connection closed");
			// <-- TA01 end
		} catch (Exception e) {
			log.error("SQL-Error while update free space information");
		}
			
	}
}
